<?php
/**
 * 分析系统规则并生成接口映射表
 * 通过查询oa_admin_rule表获取完整的权限规则并分析
 */

// 引入自动加载文件
require_once 'vendor/autoload.php';

// 数据库配置
$config = [
    'hostname' => '116.62.146.217',
    'database' => 'oa',
    'username' => 'root',
    'password' => 'JyDmXg0407',
    'hostport' => '3306',
    'prefix'   => 'oa_',
    'charset'  => 'utf8mb4',
];

try {
    // 创建数据库连接
    $dsn = "mysql:host={$config['hostname']};port={$config['hostport']};dbname={$config['database']};charset={$config['charset']}";
    $pdo = new PDO($dsn, $config['username'], $config['password'], [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ]);

    // 查询oa_admin_rule表
    $stmt = $pdo->query("SELECT * FROM oa_admin_rule ORDER BY pid, id");
    $rules = $stmt->fetchAll();

    echo "========================================\n";
    echo "系统功能菜单和操作按钮信息分析\n";
    echo "========================================\n";
    echo "总共找到 " . count($rules) . " 条规则\n\n";

    // 按模块分组规则
    $modules = [];
    $ruleMap = [];
    
    // 建立ID到规则的映射
    foreach ($rules as $rule) {
        $ruleMap[$rule['id']] = $rule;
    }
    
    // 按模块分组
    foreach ($rules as $rule) {
        $module = $rule['module'];
        if (!isset($modules[$module])) {
            $modules[$module] = [];
        }
        $modules[$module][] = $rule;
    }
    
    // 输出各模块的接口信息
    foreach ($modules as $moduleName => $moduleRules) {
        echo "【" . getModuleName($moduleName) . "】模块接口:\n";
        
        // 获取该模块的所有操作接口
        $apiEndpoints = [];
        foreach ($moduleRules as $rule) {
            // 只处理有src路径的规则（即具体的操作接口）
            if (!empty($rule['src']) && $rule['menu'] == 2) {
                $apiEndpoints[] = [
                    'title' => $rule['title'],
                    'name' => $rule['name'],
                    'src' => $rule['src'],
                    'module' => $rule['module']
                ];
            }
        }
        
        // 按src排序并输出
        usort($apiEndpoints, function($a, $b) {
            return strcmp($a['src'], $b['src']);
        });
        
        foreach ($apiEndpoints as $endpoint) {
            echo "  - {$endpoint['title']} ({$endpoint['name']}): {$endpoint['src']}\n";
        }
        
        echo "\n";
    }
    
    // 保存结构化数据到文件
    $structuredData = [
        'total_rules' => count($rules),
        'modules' => $modules
    ];
    
    file_put_contents('structured_rules.json', json_encode($structuredData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
    echo "结构化规则已保存到 structured_rules.json 文件中\n";

} catch (PDOException $e) {
    echo "数据库连接错误: " . $e->getMessage() . "\n";
} catch (Exception $e) {
    echo "错误: " . $e->getMessage() . "\n";
}

/**
 * 获取模块中文名称
 */
function getModuleName($module) {
    $moduleNames = [
        'home' => '系统管理',
        'base' => '基础数据',
        'user' => '人事管理',
        'adm' => '行政办公',
        'oa' => '办公OA',
        'finance' => '财务管理',
        'customer' => '客户管理',
        'contract' => '合同管理',
        'project' => '项目管理',
        'disk' => '知识网盘',
        'lawcase' => '案件管理',
        'lawyer' => '律所管理'
    ];
    
    return isset($moduleNames[$module]) ? $moduleNames[$module] : $module;
}